package lxj;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class User3 {
    private int score;

    private int age;

    public User3(int score, int age){
        super();
        this.score = score;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User3{" +
                "score=" + score +
                ", age=" + age +
                '}';
    }


    public static void main(String[] args) {
        List<User> users = new ArrayList<User>();
        users.add(new User(78, 26));
        users.add(new User(67, 23));
        users.add(new User(34, 56));
        users.add(new User(55, 23));

        //sort(List<T> list)
        //根据元素的自然顺序 对指定列表按升序进行排序。
        //自定义对象没有自然顺序,就不排序
        Collections.sort(users);
        for(User user : users){
            System.out.println(user.getScore() + "," + user.getAge());
        }


        //包装类型会升序排序  包装类型的源码应该实现了Comparable接口
        List<Integer>  list = new ArrayList<Integer>();
        list.add(1);
        list.add(3);
        list.add(2);
        Collections.sort(list);
        System.out.println(list);


        //自定义降序排序
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return -1;
            }
        });
        System.out.println(list);


        //字符串类型也会自动升序排序
        List<String>  list2 = new ArrayList<String>();
        list2.add("a");
        list2.add("c");
        list2.add("b");
        Collections.sort(list2);
        System.out.println(list2);



        //自定义降序
        Collections.sort(list2, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.hashCode()-o1.hashCode();
            }
        });

        System.out.println(list2);
    }
}
